package com.jsmtr.rms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Sheet;
import jxl.Workbook;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.service.IExUsersService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.StringTools;

@Controller
@RequestMapping("/ExUser")
public class ExUsersController {

    @Resource
    private IUtilService utilService;
    @Resource
    private IExUsersService exUsersService;

    //[start] 信息管理
    @RequestMapping("/")
    public String Index(HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ExUser/index";
    }

    @SuppressWarnings("unchecked")
    @RequestMapping("/getusers")
    public void getusers(HttpServletRequest request, HttpServletResponse response, Ex_Users user) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));

            Map<String, Object> map = exUsersService.getExUsers(page, perpage, user);
            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");
            Integer rowNum=1;
            if (pageCount != 0) {
                Object object = map.get("list");
                for (Ex_Users _user : (List<Ex_Users>) object) {
                    result.append("<tr><td><input type='checkbox' id='cb_" + _user.getId() + "' value='" + _user.getId() + "' /></td>");
                    result.append("<td>" + rowNum + "</td>");
                    result.append("<td>" + _user.getCode() + "</td>");
                    result.append("<td>" + _user.getName() + "</td>");
                    result.append("<td>" + (_user.getSex() == null ? "&nbsp;" : _user.getSex()) + "</td>");
                    result.append("<td>" + (_user.getXueli() == null ? "&nbsp;" : _user.getXueli()) + "</td>");
                    result.append("<td>" + (_user.getSchool() == null ? "&nbsp;" : _user.getSchool()) + "</td>");
                    result.append("<td>" + (_user.getSxzy() == null ? "&nbsp;" : _user.getSxzy()) + "</td>");
                    result.append("<td>" + (_user.getZpposition() == null ? "&nbsp;" : _user.getZpposition()) + "</td>");
                    result.append("<td>" + (_user.getZpflow() == null ? "&nbsp;" : _user.getZpflow()) + "</td>");
                    result.append("<td>" + (_user.getZpstate() == null ? "&nbsp;" : _user.getZpstate()) + "</td>");
                    result.append("<td>" + (_user.getZpremark() == null ? "&nbsp;" : _user.getZpremark()) + "</td></tr>");
                    rowNum++;
                }
            } else {
                result.append("<tr><td colspan='11'>未找到符合条件的数据</td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='11'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }

    }

    //[end]

    //[start] 导入管理
    @RequestMapping("/import")
    public String importUser(HttpServletRequest request, HttpServletResponse response, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ExUser/import";
    }

    @RequestMapping(value = {"/importaction"}, method = RequestMethod.POST)
    public void importAction(@RequestParam("excel") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        try {
            String exname = StringTools.getFileExtension(file.getOriginalFilename());//扩展名（.xxx）
            if (exname.equals(".xls")) {
                Workbook rwb = Workbook.getWorkbook(file.getInputStream());
                Sheet rs = rwb.getSheet(0);//得到第一个工作表中的数据(人员信息)
                int rows = rs.getRows();//得到所有的行

                List<Ex_Users> alist = new ArrayList<Ex_Users>();
                List<Ex_Users> ulist = new ArrayList<Ex_Users>();

                for (int i = 1; i < rows; i++) {
                    String code = rs.getCell(1, i).getContents().trim().replace('x', 'X');
                    Ex_Users _user = exUsersService.getExUserByCode(code);
                    if (_user == null) {
                        Ex_Users user = new Ex_Users();
                        user.setId(UUID.randomUUID().toString());
                        user.setCode(code);
                        user.setName(rs.getCell(2, i).getContents().trim());
                        user.setSex(rs.getCell(3, i).getContents().trim());
                        user.setBirthday(rs.getCell(4, i).getContents().trim());
                        user.setRklx(rs.getCell(5, i).getContents().trim());
                        user.setXueli(rs.getCell(6, i).getContents().trim());
                        user.setSchool(rs.getCell(7, i).getContents().trim());
                        user.setSxzy(rs.getCell(8, i).getContents().trim());
                        user.setBysj(rs.getCell(9, i).getContents().trim());
                        user.setZcname(rs.getCell(10, i).getContents().trim());
                        user.setZczy(rs.getCell(11, i).getContents().trim());
                        user.setZctime(rs.getCell(12, i).getContents().trim());
                        user.setZgname(rs.getCell(13, i).getContents().trim());
                        user.setZgtype(rs.getCell(14, i).getContents().trim());
                        user.setZgtime(rs.getCell(15, i).getContents().trim());
                        user.setTel(rs.getCell(16, i).getContents().trim());
                        user.setZpplan(rs.getCell(17, i).getContents().trim());
                        user.setZpposition(rs.getCell(18, i).getContents().trim());
                        user.setZpflow(rs.getCell(19, i).getContents().trim());
                        user.setZpstate(rs.getCell(20, i).getContents().trim());
                        user.setZpremark(rs.getCell(21, i).getContents().trim());

                        alist.add(user);
                    } else {
                        _user.setName(rs.getCell(2, i).getContents().trim());
                        _user.setSex(rs.getCell(3, i).getContents().trim());
                        _user.setBirthday(rs.getCell(4, i).getContents().trim());
                        _user.setRklx(rs.getCell(5, i).getContents().trim());
                        _user.setXueli(rs.getCell(6, i).getContents().trim());
                        _user.setSchool(rs.getCell(7, i).getContents().trim());
                        _user.setSxzy(rs.getCell(8, i).getContents().trim());
                        _user.setBysj(rs.getCell(9, i).getContents().trim());
                        _user.setZcname(rs.getCell(10, i).getContents().trim());
                        _user.setZczy(rs.getCell(11, i).getContents().trim());
                        _user.setZctime(rs.getCell(12, i).getContents().trim());
                        _user.setZgname(rs.getCell(13, i).getContents().trim());
                        _user.setZgtype(rs.getCell(14, i).getContents().trim());
                        _user.setZgtime(rs.getCell(15, i).getContents().trim());
                        _user.setTel(rs.getCell(16, i).getContents().trim());
                        _user.setZpplan(rs.getCell(17, i).getContents().trim());
                        _user.setZpposition(rs.getCell(18, i).getContents().trim());
                        _user.setZpflow(rs.getCell(19, i).getContents().trim());
                        _user.setZpstate(rs.getCell(20, i).getContents().trim());
                        _user.setZpremark(rs.getCell(21, i).getContents().trim());

                        ulist.add(_user);
                    }
                }

                utilService.addList(alist);
                utilService.editList(ulist);
                utilService.printJsonResult(true, "操作成功", "no", response);

            } else {
                utilService.printJsonResult(false, "只能上传后缀为：xls的Excel文件", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "导入失败", "no", response);
        }

    }

    //[end]

    //[start] 删除信息

    @RequestMapping("/DelAction")
    public void DelAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");
            utilService.delList(exUsersService.getExUsers(ids));

            utilService.printJsonResult(true, "操作成功", "no", response);

        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败", "no", response);
        }

    }

    //[end]
}
